
# set target here
TARGET:= appmon
VERSION := 0.3

# build options
GO_OPTIONS := GO111MODULE=off

ifneq ($(BUILD_ARCH),)
GO_OPTIONS += GOARCH=$(BUILD_ARCH)
endif 

ifneq ($(EVB),)
extra_targets := install_to_evb
endif 

EVB_DIR :=$(EVB):/usr/sbin/

# generate version

NOW_SECOND=$(shell date -d "now +8 hour" +"%s")
V_TIME=$(shell expr $(NOW_SECOND) - 28800)
V_DATE=$(shell date +"%Y%m%d-%H%M" -d @$(V_TIME))
BUILD_VERSION := V$(VERSION)_$(V_DATE)

# set go build with options 
GO := $(GO_OPTIONS) go

GOFLAGS=-ldflags "-X gitee.com/eehut/appmonitor-go.Version=$(BUILD_VERSION)"

all: clean build $(extra_targets)

run: clean build 
	./$(TARGET)

build: $(TARGET)

$(TARGET):
	@echo BUILD_VERSION=$(BUILD_VERSION)
	$(GO) build $(GOFLAGS) -o $@ ./cmd

install_to_evb:
	@echo "Install $(TARGET) to $(EVB_DIR)" 
	scp $(TARGET) $(EVB_DIR)

clean:
	rm -f $(TARGET)

help:
	@echo "Cross-build:"
	@echo "> make BUILD_ARCH=arm64"
	@echo "Install to Board:"
	@echo "> make EVB=root@192.168.100.3"
	@echo " -- "
	@echo "Set variables to envs"
	@echo "> export EVB=root@192.168.100.3 BUILD_ARCH=arm64"
	@echo "> make "
	@echo "Byebye!"
